package src.view.gl;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import src.domain.gl.Glbalance;
import src.services.gl.GledgerModel;
import src.services.system.BranchModel;
import src.util.Utils;

/**
 *
 * @author Lalugina
 */
@ManagedBean
@SessionScoped
public class TrialBalanceBean {

    private List<SelectItem> avblBranch, avblPeriod;
    private List<Glbalance> glbalances;
    private Date startDate, endDate, maxDate, minDate;
    private Integer branchId, period, year;
    private String sumDebit, sumCredit;
    
    public TrialBalanceBean() {
        
        this.avblBranch = new BranchModel().listAll();
        this.avblPeriod = Utils.getMonths();
        this.glbalances = new ArrayList<>();
        
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        startDate = cal.getTime();
        minDate = cal.getTime();
        
        Calendar cal2 = Calendar.getInstance();;
        int lastDate = cal2.getActualMaximum(Calendar.DATE);
        cal2.set(Calendar.DATE, lastDate);
        endDate = cal2.getTime();
        maxDate = cal2.getTime();
        
        period = cal.get(Calendar.MONTH)+1;
        year = cal.get(Calendar.YEAR);
    }

    public Integer getBranchId() {
        return branchId;
    }

    public void setBranchId(Integer branchId) {
        this.branchId = branchId;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Integer getPeriod() {
        return period;
    }

    public void setPeriod(Integer period) {
        this.period = period;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public List<SelectItem> getAvblBranch() {
        return avblBranch;
    }

    public List<SelectItem> getAvblPeriod() {
        return avblPeriod;
    }

    public List<Glbalance> getGlbalances() {
        return glbalances;
    }

    public Date getMaxDate() {
        return maxDate;
    }

    public Date getMinDate() {
        return minDate;
    }
    
    public void changeDate() {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, period-1, Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, 1);

        startDate = cal.getTime();
        minDate = cal.getTime();

        Calendar cal2 = Calendar.getInstance();
        cal2.clear();
        cal2.set(year, period-1, Calendar.DAY_OF_MONTH);
        int lastDate = cal2.getActualMaximum(Calendar.DATE);
        cal2.set(Calendar.DATE, lastDate);

        endDate = cal2.getTime();
        maxDate = cal2.getTime();
    }
    
    public void loadData() {
        this.glbalances = new GledgerModel().loadDataGLBalanceTrial(branchId, startDate, endDate);
        
        BigDecimal totalDebit = BigDecimal.ZERO;
        BigDecimal totalCredit = BigDecimal.ZERO;
        
        for (int i=0; i<glbalances.size(); i++) {
            totalDebit = totalDebit.add(glbalances.get(i).getTotal01());
            totalCredit = totalCredit.add(glbalances.get(i).getTotal02());
        }
        sumDebit = Utils.formatNumber(totalDebit);
        sumCredit = Utils.formatNumber(totalCredit);
    }

    public String getSumCredit() {
        return sumCredit;
    }

    public String getSumDebit() {
        return sumDebit;
    }
    
    
    
}
